﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HMS_Hospital_Team2._6.DA
{
    class PhongBanDA
    {
        // DA
        HMSHospitalEntities context = new HMSHospitalEntities();
        PubicLibDA pubPB = new PubicLibDA();

        // Hàm load Data lên gridview
        public DataTable LoadDuLieuDA()
        {
            var PhongBan = from Pb in context.PhongBans
                           select new
                           {
                               Pb.MaPhongBan,
                               Pb.TenPhongBan,
                               Pb.NhomDichVu.TenNhomDV,
                               Pb.GhiChu,
                               Pb.TrangThai
                           };
            return pubPB.LinqToDataTable(PhongBan);
        }

        public DataTable LoadDuLieu_Banthu(string sMaphongBan)
        {
            var banthu  = from bt in context.BanThus
                          where bt.MaPhongBan == sMaphongBan
                       select new
                       {
                           bt.TenBan,
                           bt.MaBan
                       };
            return pubPB.LinqToDataTable(banthu);
        }

        // Hàm load Data tên loại PB lên Combobox
        public DataTable LoadDuLieuTlpb()
        {
            var Tlpb = from lpb in context.LoaiPhongBans
                       select new
                       {
                           lpb.TenLoaiPhong,
                           lpb.MaLoaiPhong
                       };
            return pubPB.LinqToDataTable(Tlpb);
        }

        // Hàm load Data tên nhóm DV lên Combobox
        public DataTable LoadDuLieuTnDV()
        {
            var Tndv = from ndv in context.NhomDichVus
                       select new
                       {
                           ndv.TenNhomDV,
                           ndv.MaNhomDV
                       };
            return pubPB.LinqToDataTable(Tndv);
        }

        // Hàm Tạo mới phòng ban
        public static void TaoMoiPB(string sMaPB,string MaLpb, string TenPb, string MaNdv, string Ghichu, bool Trangthai)
        {
            using (HMSHospitalEntities ThemMoi = new HMSHospitalEntities())
            {
                PhongBan pb = new PhongBan();
                pb.MaPhongBan = sMaPB;
                pb.MaLoaiPhong = MaLpb;
                pb.TenPhongBan = TenPb;
                pb.MaNhomDV = MaNdv;
                pb.GhiChu = Ghichu;
                pb.TrangThai = Trangthai;

                ThemMoi.AddToPhongBans(pb);
                ThemMoi.SaveChanges();
            }
        }

        // Hàm Sửa phòng ban
        public static void SuaPB(string sMaPB, string MaLpb, string TenPb, string MaNdv, string Ghichu, bool Trangthai)
        {
            using (HMSHospitalEntities Sua = new HMSHospitalEntities())
            {
                var Phongban = (from pb in Sua.PhongBans
                                where pb.MaPhongBan == sMaPB
                                select pb).First();
                Phongban.MaPhongBan = sMaPB;
                Phongban.MaLoaiPhong = MaLpb;
                Phongban.TenPhongBan = TenPb;
                Phongban.MaNhomDV = MaNdv;
                Phongban.GhiChu = Ghichu;
                Phongban.TrangThai = Trangthai;
                Sua.SaveChanges();

            }
        }

    }
}
